<?php namespace Admin\Controller; 

use Hdphp\Controller\Controller;

//测试控制器
class GlistController extends CommonController{
	private $goodsModel;
	private $glistModel;
	private $gattrModel;
	private $attrModel;
	//构造函数
	public function __init()
	{
		parent::__init();
		$this->goodsModel = new \Common\Model\Goods;
		$this->glistModel = new \Common\Model\Glist;
		$this->gattrModel = new \Common\Model\Gattr;
		$this->attrModel = new \Common\Model\Attr;
	}
	
//	默认显示页
	public function index(){
//		获得商品id
		$gid = Q('get.gid',0,'intval');
		
		if(IS_POST){
			if(!$this->glistModel->store()) View::error($this->glistModel->getError());
			View::success('添加成功',U('Glist/index',array('gid'=>$gid)));
			/*$str =<<<str
			<script type="text/javascript" language="javascript">
					alert('添加成功！');
					history.go(-1);
		    </script>
str;
			echo $str;*/
		}
//		获得类型id
		$tid = $this->goodsModel->where("gid = {$gid}")->pluck("type_tid");
//		获得atid
		$tmp = $this->attrModel->where("type_tid = {$tid} and class=1")->field('atid')->get();
//		得到atid数组
		$atid = array();
		foreach ($tmp as $k => $v) {
			$atid[] = $v['atid'];
		}
//		定义空数组用来接受数据
		$spec = array();
		foreach ($atid as $k => $v) {
//			查询所需要的内容
			$data = $this->gattrModel->join('attribute','atid','=','attribute_atid')->where("goods_gid = {$gid} and atid = {$v}")->field('gaid,gavalue,atname')->get();
//			将数组进行合并
			foreach ($data as $kk => $vv) {
				$spec[$v]['gaid'][]=$vv['gaid'];
				$spec[$v]['gavalue'][]=$vv['gavalue'];
				$spec[$v]['atname']=$vv['atname'];
			}
		}
		View::with('spec',$spec);
		
		
//		数据载入
		$glData = $this->glistModel->where("goods_gid = {$gid}")->get();
		foreach ($glData as $k => $v) {
			$v['combine'] = explode(',', $v['combine']);
			foreach ($v['combine'] as $kk => $vv) {
				$glData[$k]['comb'][$vv] = $this->gattrModel->where("goods_gid = {$v['goods_gid']} And gaid = {$vv}")->pluck('gavalue');
			}
		}
		View::with('glData',$glData);
		
//		随机货号
		$num = 'qwertyuiopasdfghjklzxcvbnm1234567890';
		static $itemno = '';
		for ($i=0; $i < 12 ; $i++) { 
			$j = mt_rand(0, strlen($num)-1);
			$itemno = $itemno .$num[$j];
		}
		View::with('itemno',$itemno);
//		载入模板
		View::make();
	}
	
//	编辑
	public function edit(){
//		接受id
		$gid = Q('get.gid',0,'intval');
		$glid = Q('get.glid',0,'intval');
//		修改内容
		if(IS_POST){
			if(!$this->glistModel->edit()) View::error($this->glistModel->getError());
			View::success('修改成功',U('Glist/index',array('gid'=>$gid)));
		}
		
		
		
		
//		获得类型id
		$tid = $this->goodsModel->where("gid = {$gid}")->pluck("type_tid");
		
//		调取glist的信息内容
//		获得atid
		$tmp = $this->attrModel->where("type_tid = {$tid} and class=1")->field('atid')->get();
//		得到atid数组
		$atid = array();
		foreach ($tmp as $k => $v) {
			$atid[] = $v['atid'];
		}
//		定义空数组用来接受数据
		$spec = array();
		foreach ($atid as $k => $v) {
//			查询所需要的内容
			$data = $this->gattrModel->join('attribute','atid','=','attribute_atid')->where("goods_gid = {$gid} and atid = {$v}")->field('gaid,gavalue,atname')->get();
//			将数组进行合并
			foreach ($data as $kk => $vv) {
				$spec[$v]['gaid'][]=$vv['gaid'];
				$spec[$v]['gavalue'][]=$vv['gavalue'];
				$spec[$v]['atname']=$vv['atname'];
			}
		}
		View::with('spec',$spec);

		$glData = $this->glistModel->where("glid={$glid} AND goods_gid = {$gid}")->find();
		$glData['combine'] = explode(',', $glData['combine']);
		View::with('glData',$glData);
		
		View::make();
	}
	
	
	
	
//	删除
	public function del(){
		$glid = Q('get.glid',0,'intval');
		$gid = Q('get.gid',0,'intval');
		$this->glistModel->where("glid = {$glid}")->delete();
		View::success('删除成功！',U('Glist/index',array('gid'=>$gid)));
	}
	
}
